package com.yndsoft.jc.ui.categorypage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.MemberPoint;
import com.yndsoft.jc.model.Product;
import com.yndsoft.jc.model.ProductType;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.netutils.DensityUtil;
import com.yndsoft.jc.ui.categorypage.adapter.ProductListAdapter;
import com.yndsoft.jc.ui.categorypage.entity.ProductListBean;
import com.yndsoft.jc.ui.memberpage.score.ScoreDetailActivity;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.ui.shoppingcart.entity.CartBean;
import com.yndsoft.jc.utils.ConstantValues;
import com.yndsoft.jc.widget.CustomDialog;
import com.yndsoft.jc.widget.SlideRecycleView;
import com.yndsoft.jc.widget.SlideRecycleViewAdapter;
import com.yndsoft.jc.widget.SlideViewHolder;
import com.yndsoft.jc.widget.pulltorefresh.PullToRefreshBase;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/5.
 */

public class ProductListActivity extends BaseActivity implements View.OnClickListener, SlideRecycleViewAdapter.OnItemClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.productList)
    SlideRecycleView productList;
    List<Product> mData = new ArrayList<>();
    ProductRecyAdapter productRecyAdapter;
    private int productId;

    private int start = 0;
    private TextView loadAllView;
    int returnCode = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        productId = getIntent().getIntExtra(ConstantValues.PRODUCT_ID, 100002);

        setViews();
        getData( start);
        setListener();
    }


    private void setViews() {

        productList.setScrollingWhileRefreshingEnabled(false); //设置刷新的时候不可滑动

        productList.getRefreshableView().setLayoutManager(new LinearLayoutManager(this)); //添加布局管理器
        productList.setMode(PullToRefreshBase.Mode.BOTH);
        productList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
//                mScoreAdapter.reMoveFooterView(loadAllView);
                loadAllView.setVisibility(View.GONE);
                start = 0;
                getData( start);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
//                mScoreAdapter.reMoveFooterView(loadAllView);
                if (returnCode == 1) {
                    start++;
                    getData( start);
                } else if (returnCode == 2) {
                    loadAllView.setVisibility(View.VISIBLE);
                    productList.onRefreshComplete();
                }

//                if (haveAnother) {
//
//                } else {
//                    mRecycleView.onRefreshComplete();
//                    mScoreAdapter.addFooterView(loadAllView);
//                }
            }
        });
        loadAllView = new TextView(this);
        loadAllView.setPadding(0, DensityUtil.dip2px(this, 10), 0, DensityUtil.dip2px(this, 10));
        loadAllView.setGravity(Gravity.CENTER);
        loadAllView.setTextSize(15);
        loadAllView.setText("已加载全部");
        productRecyAdapter = new ProductRecyAdapter(this, mData, R.layout.item_prodct_list);
        loadAllView.setVisibility(View.GONE);
        productRecyAdapter.addFooterView(loadAllView);
        productRecyAdapter.setOnItemClickListener(this);
        productList.getRefreshableView().setAdapter(productRecyAdapter);
    }

    private void getData( int start) {
        mHttpUtil.getProductListData(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:

                        break;
                    case 1://有下一页
                        returnCode = 1;
                        break;
                    case 2://只有一页
                        returnCode = 2;
                        if (null != productList) {
                            productList.onRefreshComplete();
                        }
                        Type type1 = new TypeToken<List<Product>>() {
                        }.getType();
                        Object fromJson1 = new Gson().fromJson(returnMsg.getInfo(), type1);
                        mData = (ArrayList<Product>) fromJson1;
                        productRecyAdapter.setDatas(mData);
                        productList.onRefreshComplete();
                        break;
                }

            }
        }, "productlist", productId, start, 10);
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
//        productList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                Intent intent = new Intent(ProductListActivity.this, ProductDetailActivity.class);
//                intent.putExtra(ConstantValues.ID_AD, mData.get(position).getId());
//                startActivity(intent);
////                dialog.show();
//            }
//        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
        }
    }

    @Override
    public void onItemClickListener(int posotion) {
        Intent intent = new Intent(ProductListActivity.this, ProductDetailActivity.class);
        intent.putExtra(ConstantValues.ID_AD, mData.get(posotion).getId());
        startActivity(intent);
    }

    enum RefreshType {
        PULL_DWON_TO_REFRESH,
        PULL_UP_TO_REFRESH
    }

    class ProductRecyAdapter extends SlideRecycleViewAdapter<Product> {


        /**
         * 构造器
         *
         * @param context
         * @param mDatas
         * @param itemLayoutId
         */
        protected ProductRecyAdapter(Context context, List<Product> mDatas, int itemLayoutId) {
            super(context, mDatas, itemLayoutId);
        }

        @Override
        public void convert(SlideViewHolder holder, final Product item) {
            ImageView product_image = holder.findViewById(R.id.product_image);
            TextView product_title = holder.findViewById(R.id.product_title);
            TextView product_price = holder.findViewById(R.id.product_price);

            Glide.with(mContext).load(item.getImg()).placeholder(R.mipmap.logo).into(product_image);
            AppUtils.setText(product_title, item.getSubname());
            AppUtils.setText(product_price, "￥" + item.getPrice());
        }

    }

}
